package equalityclass;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

public class PhoneBill {
    private final long limitTime = 30;
    private final long oneHourToMin = 60;
    private final long oneMinToSecond = 60;
    private final long oneSecondToMilliseconds = 1000;
    private final long turingTime = 20;
    private final double firstPrice = 0.05;
    private final double turingPrice = 1;
    private final double secondPrice = 0.1;
    //计算通话时间
    public long calculateTimeSpan(final LocalDateTime startTime, final boolean inDayLightSavingOfStartTime,
                                  final LocalDateTime endTime, final boolean inDayLightSavingOfEndTime) {
        //如果开始不是夏令时、结束是夏令时
        LocalDateTime startTimeNew = startTime;
        LocalDateTime endTimeNew = endTime;
        if (!inDayLightSavingOfStartTime && inDayLightSavingOfEndTime) {
            startTimeNew = startTime;
            endTimeNew = endTime.minusHours(1);
        } else if (inDayLightSavingOfStartTime && !inDayLightSavingOfEndTime) {
            //如果开始是夏令时、结束不是夏令时
            startTimeNew = startTime.minusHours(1);
            endTimeNew = endTime;
        }

        java.time.Duration duration = java.time.Duration.between(startTimeNew, endTimeNew);
        //时间差（分钟、毫秒）
        long durationMinutes = duration.toMinutes();
        long durationMilliseconds = duration.toMillis();

        //判断输入时间是否合法
        if (durationMilliseconds < 0 || durationMilliseconds > limitTime * oneHourToMin
                                            * oneMinToSecond * oneSecondToMilliseconds) {
            return -1;
        } else {
            //判断是否有秒进位到分钟
            long durationMillisecondsByMinutes = durationMinutes * oneMinToSecond * oneSecondToMilliseconds;
            if (durationMilliseconds > durationMillisecondsByMinutes) {
                durationMinutes++;
            }
            return durationMinutes;
        }
    }

    //计算通话费用
    public BigDecimal calculateFee(final long timeLength) {
        double money;
        //输入时间不合法
        if (timeLength == -1) {
            return new BigDecimal(-1);
        } else {
            //计算费用
            if (timeLength <= turingTime) {
                money = firstPrice * timeLength;
            } else {
                money = turingPrice + secondPrice * (timeLength - turingTime);
            }
            BigDecimal bigDecimal = BigDecimal.valueOf(money);
            BigDecimal finalMoney = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
            return finalMoney;
        }
    }

    public String fee(final String startTime, final boolean inDayLightSavingOfStartTime, final String endTime,
                      final boolean inDayLightSavingOfEndTime) {
        //转变输入类型为LocalDateTime
        LocalDateTime startTimeFormat = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        LocalDateTime endTimeFormat = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        long timeLength = this.calculateTimeSpan(startTimeFormat, inDayLightSavingOfStartTime,
                                                endTimeFormat, inDayLightSavingOfEndTime);
        BigDecimal fee = this.calculateFee(timeLength);
        if (Objects.equals(fee, new BigDecimal(-1))) {
            return "输入时间有误！";
        } else {
            return "费用为：" + fee + "美元";
        }
    }
}
